package com.morgankelly.action;

import java.util.ArrayList;
import java.util.List;

import com.morgankelly.po.Account;
import com.morgankelly.util.Constants;
import com.morgankelly.util.RoleEnum;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class AuthAction extends BaseAction {
    
	public String input() {
		
		initSideBar();
		return INPUT;
	}
	
    public String login() {

        //If account is null,  of course. it is Invalid.
		if (name == null || name.equals("")) {
			addActionError(getText(Constants.ERROR_ACCOUNT_IS_EMPTY));
			return FAILED;
		}
        
    	Account account = getAuthService().getAccountByName(name);

        List<Object> args = new ArrayList<Object>();
        if(account == null) {
            args.add(name);
            addActionError(getText(Constants.ERROR_ACCOUNT_NOT_EXIST, args));
            return FAILED;
            
        } else {
        	if (password.equals(account.getPassword())) {
                ActionContext.getContext().getSession().put("name", account.getName());
                ActionContext.getContext().getSession().put("displayName", account.getDisplayName());
                return LOGIN;
        	} else {
                args.add(name);
                addActionError(getText(Constants.ERROR_INVALID_PASSWORD, args));
                return FAILED;
        	}
        }
    }
	public String logout() {

		//Remove name and displayName from session.
        ActionContext.getContext().getSession().remove("name");
        ActionContext.getContext().getSession().remove("displayName");

		return LOGOUT;
	}
	
	public String registerInput() {
		
		//By default, everyone only can register as "User";
		setRoleName(RoleEnum.User.name());
		
		initSideBar();
		return REGISTER_INPUT;
	}
	
	public String register() {
		
	    Account account = new Account();
	    account.setName(name);
	    account.setPassword(password);
	    account.setDisplayName(displayName);
	    account.setEmail(email);
	    account.setRole(RoleEnum.User.name());
	    account.setPicPath(picPath);

	    getAuthService().addAccount(account);

        ActionContext.getContext().getSession().put("name", name);
        ActionContext.getContext().getSession().put("displayName", displayName);
	    
        List<Object> args = new ArrayList<Object>();
        args.add(name);
	    addActionMessage(getText(Constants.MESSAGE_REGISTER_SUCCESS, args));

		return REGISTER;
	}

	private String name;
	private String password;
	private String displayName;
	private String email;
	private String roleName;
	private String picPath;
	
	private boolean rememberMe;

	public boolean isRememberMe() {
		return rememberMe;
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}
}
